package cn.wolfcode.lww.controller;

import cn.wolfcode.lww.domain.Comment;
import cn.wolfcode.lww.domain.Travelnotes;
import cn.wolfcode.lww.service.CommentService;
import cn.wolfcode.lww.service.impl.CommentServiceImpl;
import cn.wolfcode.lww.service.impl.TravelnotesServiceImpl;
import cn.wolfcode.lww.util.QueryObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Controller
public class TravelnotesController {
    @Autowired
    TravelnotesServiceImpl travelnotesService;
    @Autowired
    CommentServiceImpl commentService;
    @RequestMapping("/selectTravelNotesAll")
    public String selectTravelNotesAll(Model model, QueryObject queryObject){
        PageInfo<Travelnotes> travelnotesPageInfo = travelnotesService.selectAll(queryObject);
        System.out.println(travelnotesPageInfo.getList().get(1).getCover());
        model.addAttribute("list",travelnotesPageInfo);
        return "travel/travel_journal";
    }
    @RequestMapping("/selectTravelByPrimaryKey")
    public String selectTravelByPrimaryKey(String id,Model model){
        Travelnotes travelnotes = travelnotesService.selectAllByIdAfter(id);
        System.out.println(travelnotes.toString());
        model.addAttribute("travelnote",travelnotes);
        List<Comment> comments = commentService.selectByArtIdAfter(id);
        model.addAttribute("commentList",comments);
        return "travel/travel_journal_content";
    }
    @RequestMapping("pushTravelNotes")
    public String pushTravelNotes(String id,Model model){
        if (id!=null){
            Travelnotes travelnotes = travelnotesService.selectAllByIdAfter(id);
            model.addAttribute("travelnotes",travelnotes);

        }
        String type="insert";
        model.addAttribute("type",type);
        return "travel/push";
    }
    @RequestMapping("/updateTravelNotes")
    public String updateTravelNotes(Travelnotes travelnotes){
        int i = travelnotesService.updateByPrimaryKey(travelnotes);
        return "redirect:/selectTravelNotesAll";
    }
    @RequestMapping("/insertTravelNotes")
    public String insertTravelNotes(Travelnotes travelnotes){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        travelnotes.setWriteTime(format);
        travelnotesService.insert(travelnotes);
        return "redirect:/selectTravelNotesAll";
    }
}
